package cn.tsoft.taobao.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

public class LogInInfo {
	
	 
	/**
	 * 请求授权服务获得当前登录用户的唯一标识key
	 * 用户授权用的 用户登录用的网页url
	 * @author wanggj
	 */
	public static void getAuthorInfo(){
		    AuthorInfo authorInfo = AuthorInfo.getInstance();
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("api", "com.taobao.wireless.mtop.getLoginUrl"));
			params.add(new BasicNameValuePair("v", "*"));
			params.add(new BasicNameValuePair("imei",authorInfo.imei) );
			params.add(new BasicNameValuePair("imsi", authorInfo.imsi));
			params.add(new BasicNameValuePair("appkey", AuthorInfo.APP_KEY));
			params.add(new BasicNameValuePair("ttid", AuthorInfo.TTID));
			params.add(new BasicNameValuePair("t",System.currentTimeMillis()+""));
			params.add(new BasicNameValuePair("sign",AuthorInfo.APP_SERCET));
			StringBuffer data = new StringBuffer("{");
			data.append("\"appkey\":\""+AuthorInfo.APP_KEY+"\",");
			data.append("\"callbackUrl\":\""+AuthorInfo.MTOP_URl+"\"}");
			params.add(new BasicNameValuePair("data",data.toString()));
			HttpEngine httpEngine  =new HttpEngine();
			httpEngine.getHttpClient();
			String  temp_SearchedDate  =  httpEngine.doPost(AuthorInfo.LOGIN_URL, params);
		    try{
		    	JSONObject json =new JSONObject(temp_SearchedDate);
		    	String urlData = json.getString("data") ;
		    	JSONObject  dataJson =new JSONObject(urlData);
		    	authorInfo.authorKey= dataJson.getString("key");
		    	authorInfo.authorUrl=dataJson.getString("url");
		    }catch(Exception ex){}
	}
	
	/**
	 * 用于获取wap session, top session和token。
	 * @author wanggj
	 */
	public static void getUserSessionKey(){
			AuthorInfo authorInfo = AuthorInfo.getInstance();
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("api", "com.taobao.client.mtop.getUserSessionKey"));
			params.add(new BasicNameValuePair("v", "*"));
			params.add(new BasicNameValuePair("imei",authorInfo.imei) );
			params.add(new BasicNameValuePair("imsi", authorInfo.imsi));
			params.add(new BasicNameValuePair("appkey", AuthorInfo.APP_KEY));
			params.add(new BasicNameValuePair("ttid", AuthorInfo.TTID));
			params.add(new BasicNameValuePair("t",System.currentTimeMillis()+""));
			params.add(new BasicNameValuePair("sign",AuthorInfo.APP_SERCET));
			StringBuffer data = new StringBuffer("{");
			data.append("\"key\":\""+authorInfo.authorKey+"\",");
			data.append("\"appkey\":\""+AuthorInfo.APP_KEY+"\"}");
			params.add(new BasicNameValuePair("data",data.toString()));
			HttpEngine httpEngine  =new HttpEngine();
			httpEngine.getHttpClient();
			String  temp_SearchedDate  =  httpEngine.doPost(AuthorInfo.LOGIN_URL, params);
			try{
		    	JSONObject json =new JSONObject(temp_SearchedDate);
		    	String urlData = json.getString("data") ;
		    	JSONObject  dataJson =new JSONObject(urlData);
		    	authorInfo.ecode = dataJson.getString("ecode");
		    	authorInfo.nick = dataJson.getString("nick");
		    	authorInfo.sid= dataJson.getString("sid");
		    	//自动登录字符串
		    	authorInfo.token= dataJson.getString("token");
		    	// top 平台使用的session
		    	authorInfo.topSession = dataJson.getString("topsession");
		    }catch(Exception ex){}
	}
	
	/**
	 * 自动登录
	 * @author wanggj
	 */
	public static void autoLogin(){
			AuthorInfo authorInfo = AuthorInfo.getInstance();
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("api", "com.taobao.client.sys.autologin"));
			params.add(new BasicNameValuePair("v", "*"));
			params.add(new BasicNameValuePair("imei",authorInfo.imei) );
			params.add(new BasicNameValuePair("imsi", authorInfo.imsi));
			params.add(new BasicNameValuePair("ttid", AuthorInfo.TTID));
			params.add(new BasicNameValuePair("t",System.currentTimeMillis()+""));
			params.add(new BasicNameValuePair("appkey", AuthorInfo.APP_KEY));
			params.add(new BasicNameValuePair("sign",AuthorInfo.APP_SERCET));
			StringBuffer data = new StringBuffer("{");
			data.append("\"topToken\":\""+authorInfo.token+"\",");
//			data.append("\"token\":\""+authorInfo.getToken()+"\",");
			data.append("\"appkey\":\""+AuthorInfo.APP_KEY+"\"}");
	}
	
}
